/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.Color;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.mlc.swing.layout.LayoutConstraintsManager;

import slhs.tempo.TableAnalyzer.IntegerField;

/**
 * ProbesTimingParameters.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class GenericProbeTimingPanelHandler extends JPanel implements PanelHandler {

    private static final String GENERIC_PROBE_TIMING_PANEL_LAYOUT = "GENERIC_PROBE_TIMING_PANEL_LAYOUT.XML";

    private JPanel probeTimingFormatPanel;

    private JLabel numberOfProbesLabel;
    private JLabel durationOfProbeOnScreenLabel;
    private JLabel waitForAnswerDurationLabel;
    private JLabel pauseBetweenProbesLabel;
    private JLabel randomizationNumberLabel;
    private JLabel secondProbeDurationOnScreenLabel;
    private JLabel waitForSecondAnswerDurationLabel;
    private JLabel secondPauseDurationBetweenProbesLabel;
    private JLabel delayBeforeNextSegmentLabel;

    private JTextField numberOfProbesTextField;
    private JTextField durationOfProbeOnScreenTextField;
    private JTextField waitForAnswerDurationTextField;
    private JTextField pauseBetweenProbesTextField;
    private JTextField randomizationNumberTextField;
    private JTextField secondProbeDurationOnScreenTextField;
    private JTextField waitForSecondAnswerDurationTextField;
    private JTextField secondPauseDurationBetweenProbesTextField;
    private JTextField delayBeforeNextSegmentTextField;

    private JButton addProbeDataButton;

    public GenericProbeTimingPanelHandler() {
	initComponents();
	addComponents();
    }

    /**
     * Helper function to initialize all components
     */
    private void initComponents() {

	probeTimingFormatPanel = new JPanel();

	numberOfProbesLabel = new JLabel("Number Of Probes Per Segment");
	durationOfProbeOnScreenLabel = new JLabel("Duration On Screen (millisec.)");
	waitForAnswerDurationLabel = new JLabel("Wait For Answer (millisec.)");
	pauseBetweenProbesLabel = new JLabel("Pause Between Probes (millisec.)");
	randomizationNumberLabel = new JLabel("Randomization (Optional)");
	secondProbeDurationOnScreenLabel = new JLabel("Second Probe Duration On Screen (millisec.)");
	waitForSecondAnswerDurationLabel = new JLabel("Wait For Second Answer Duration (millisec.)");
	secondPauseDurationBetweenProbesLabel = new JLabel("Second Pause Duration (millisec.)");
	delayBeforeNextSegmentLabel = new JLabel("Delay Before Next Segment (millisec.)");

	numberOfProbesTextField = new IntegerField();
	durationOfProbeOnScreenTextField = new IntegerField();
	waitForAnswerDurationTextField = new IntegerField();
	pauseBetweenProbesTextField = new IntegerField();
	randomizationNumberTextField = new IntegerField();
	secondProbeDurationOnScreenTextField = new IntegerField();
	waitForSecondAnswerDurationTextField = new IntegerField();
	secondPauseDurationBetweenProbesTextField = new IntegerField();
	delayBeforeNextSegmentTextField = new IntegerField();

	addProbeDataButton = new JButton("Add Probe Data");

	// configure components
	addProbeDataButton.addActionListener(this);
    }

    /**
     * helper method to add all the components on panel
     */
    private void addComponents() {

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(GENERIC_PROBE_TIMING_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	probeTimingFormatPanel.setLayout(panelLayout);
	// here we add the controls to the container.
	probeTimingFormatPanel.add(waitForAnswerDurationTextField, "waitForAnswerDurationTextField");
	probeTimingFormatPanel.add(secondPauseDurationBetweenProbesTextField,
		"secondPauseDurationBetweenProbesTextField");
	probeTimingFormatPanel.add(randomizationNumberLabel, "randomizationNumberLabel");
	probeTimingFormatPanel.add(waitForAnswerDurationLabel, "waitForAnswerDurationLabel");
	probeTimingFormatPanel.add(waitForSecondAnswerDurationLabel, "waitForSecondAnswerDurationLabel");
	probeTimingFormatPanel.add(numberOfProbesLabel, "numberOfProbesLabel");
	probeTimingFormatPanel.add(durationOfProbeOnScreenTextField, "durationOfProbeOnScreenTextField");
	probeTimingFormatPanel.add(secondProbeDurationOnScreenLabel, "secondProbeDurationOnScreenLabel");
	probeTimingFormatPanel.add(randomizationNumberTextField, "randomizationNumberTextField");
	probeTimingFormatPanel.add(pauseBetweenProbesLabel, "pauseBetweenProbesLabel");
	probeTimingFormatPanel.add(durationOfProbeOnScreenLabel, "durationOfProbeOnScreenLabel");
	probeTimingFormatPanel.add(secondProbeDurationOnScreenTextField, "secondProbeDurationOnScreenTextField");
	probeTimingFormatPanel.add(secondPauseDurationBetweenProbesLabel, "secondPauseDurationBetweenProbesLabel");
	probeTimingFormatPanel.add(numberOfProbesTextField, "numberOfProbesTextField");
	probeTimingFormatPanel.add(pauseBetweenProbesTextField, "pauseBetweenProbesTextField");
	probeTimingFormatPanel.add(waitForSecondAnswerDurationTextField, "waitForSecondAnswerDurationTextField");
	probeTimingFormatPanel.add(delayBeforeNextSegmentLabel, "delayBeforeNextSegmentLabel");
	probeTimingFormatPanel.add(delayBeforeNextSegmentTextField, "delayBeforeNextSegmentTextField");
	probeTimingFormatPanel.add(addProbeDataButton, "addProbeDataButton");

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {
	Object source = event.getSource();
	if (source.equals(addProbeDataButton)) {
	    // validate that text fields are not empty
	    System.out.println("Adding probe data");

	    Component[] components = probeTimingFormatPanel.getComponents();
	    for (Component currentComponent : components) {
		try {
		    JTextField currentTextfield = (JTextField) currentComponent;
		    String currentText = currentTextfield.getText();
		    if ((currentTextfield.isEnabled()) && ((currentText == null) || (currentText.equals("")))) {
			JOptionPane.showMessageDialog(null, "Value in the highlighted field is required");
			currentTextfield.setBackground(Color.YELLOW);
		    }
		} catch (Exception e) {
		    // do nothing as the exception is expected for non text field components
		}
	    }

	    // read number of probes in this segment text field and populate the combo box accordingly
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	return probeTimingFormatPanel;
    }

    /**
     * Method to disable randomization text field
     * */
    protected void disableRandomizationTextField() {
	randomizationNumberTextField.setEnabled(false);
	randomizationNumberTextField.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * Method to disable second probe duration on screen text field
     */
    protected void disableSecondProbeDurationTextField() {
	secondProbeDurationOnScreenTextField.setEnabled(false);
	secondProbeDurationOnScreenTextField.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * Method to disable wait for second answer duration on screen text field
     */
    protected void waitForSecondAnswerDurationTextField() {
	waitForSecondAnswerDurationTextField.setEnabled(false);
	waitForSecondAnswerDurationTextField.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * Method to disable pause duration after second answer
     */
    protected void secondPauseDurationBetweenProbesTextField() {
	secondPauseDurationBetweenProbesTextField.setEnabled(false);
	secondPauseDurationBetweenProbesTextField.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * Method to disable delay before next segment text field
     */
    protected void delayBeforeNextSegmentTextField() {
	delayBeforeNextSegmentTextField.setEnabled(false);
	delayBeforeNextSegmentTextField.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * 
     */
    protected void disableDurationOfProbeOnScreenTextField() {
	durationOfProbeOnScreenTextField.setEnabled(false);
	durationOfProbeOnScreenTextField.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * 
     */
    protected void disableWaitForAnswerDurationTextField() {
	waitForAnswerDurationTextField.setEnabled(false);
	waitForAnswerDurationTextField.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * 
     */
    protected void disablePauseBetweenProbesTextField() {
	pauseBetweenProbesTextField.setEnabled(false);
	pauseBetweenProbesTextField.setBackground(Color.LIGHT_GRAY);
    }

}
